/*
 *
 */
package com.ipnc.dss.util;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;

import com.ipnc.dss.config.Config;

/**
 * @author dp
 * 
 */
public class StringUtil {

	public static void main(String[] args) {
		String[] paras = new String[] { "1", "2" };
		System.out.println(StringUtil.sqlInString(paras));
	}

	public static String sqlInString(String[] paras) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < paras.length; i++) {
			sb.append("'").append(paras[i]);
			if (i < paras.length - 1) {
				sb.append("',");
			} else {
				sb.append("'");
			}
		}
		return sb.toString();
	}

	public static String[] hashSet2StringArray(HashSet<String> paras) {
		String[] ret = new String[paras.size()];
		int i = 0;
		for (String str : paras) {
			ret[i++] = str;
		}
		return ret;
	}

	public static String byte2String(byte[] data) {
		if (data == null) {
			return null;
		}
		String key = "";
		try {
			key = new String(data, Config.UTF8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return key.trim();
	}

	public static byte[] string2Byte(String data) {
		if (data == null) {
			return null;
		}
		byte[] msgByte = null;
		try {
			msgByte = data.getBytes(Config.UTF8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return msgByte;
	}

	public static boolean isValidString(String strValue) {
		if (strValue == null || strValue.equals("")
				|| strValue.equalsIgnoreCase("NULL")) {
			return false;
		}
		return true;
	}

	public static String getString(String strValue) {
		if (strValue == null || strValue.equals("")
				|| strValue.equalsIgnoreCase("NULL")) {
			return "";
		}
		return strValue.trim();
	}

}
